<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avater_box">
        <img src="../assets/logo.png" />
      </div>
      <el-form class="form" ref="LoginFormRef" :model="form" :rules="rules">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont iconuser" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont iconpassword" v-model="form.password" type="password"></el-input>
        </el-form-item>
        <div class="buttons">
          <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetFields">重置</el-button>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    resetFields() {
      this.$refs.LoginFormRef.resetFields();
    },
    login() {
      this.$refs.LoginFormRef.validate(async val => {
        if (val) {
          const { data: res } = await this.$http.post("login", this.form);
          if (res.meta.status == 200) {
            this.$message.success("登录成功");
            // 保存token
            window.sessionStorage.setItem("token", res.data.token);
            this.$router.push("/home");
          } else {
            this.$message.error("登录失败");
          }
        } else {
          return;
        }
      });
    }
  }
};
</script>
<style lang="less" scoped>
.login_container {
  position: relative;
  height: 100%;
  background-color: #2b4b6b;
}
.login_box {
  position: absolute;
  width: 450px;
  height: 300px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 3px;
  background-color: white;
  .avater_box {
    position: absolute;
    width: 130px;
    height: 130px;
    border-radius: 50%;
    border: 1px solid #eee;
    padding: 10px;
    box-shadow: 0 0 10px #eee;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .buttons {
    display: flex;
    justify-content: flex-end;
  }
  .form {
    box-sizing: border-box;
    position: absolute;
    bottom: 0px;
    width: 100%;
    padding: 10px;
  }
}
</style>
